package com.example.demo2.service.impl;

import com.example.demo2.mapper.AddressMapper;
import com.example.demo2.pojo.PageBean;
import com.example.demo2.pojo.data.AdminDO;
import com.example.demo2.utils.DataUtils;
import com.example.demo2.utils.Result;
import com.example.demo2.pojo.data.AddressDO;
import com.example.demo2.pojo.vo.AddressVO;
import com.example.demo2.service.AddressService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/** 
 * <br/>
 * Created by weidong on 2020/05/12
 */
@Service("addressService")
public class AddressServiceImpl implements AddressService {

	@Resource
	private AddressMapper addressMapper;

	@Override 	public Result add(AddressVO addressVO) throws Exception {
		Integer count = addressMapper.add(addressVO);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getSuccess("添加失败");
		}
	}

	@Override
	public Result updateById(AddressVO addressVO) throws Exception {
		Integer count = addressMapper.updateById(addressVO);
		if (count > 0) {
			return Result.getSuccess("操作成功");
		}else{
			return Result.getSuccess("操作失败");
		}
	}
	
	@Override
	public Result findById(Integer id) throws Exception{
		AddressDO addressDO = addressMapper.findById(id);
		if (addressDO != null) {
			Result result = new Result();
			result.setMsg("操作成功");
			result.setData(addressDO);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}

	@Override
	public Result list(AddressVO addressVO) throws Exception{
		List<AddressDO> addressDOs = addressMapper.list(addressVO);
		if (addressDOs != null) {
			Result result = new Result();
			result.setMsg("查询成功");
			result.setData(addressDOs);
			result.setCode(Result.CODE_SUCCESS);
			return result;
		}else{
			return Result.getFailure("操作失败");
		}
	}
	//设置每页显示几条数据
	int pageSize=10;
	//设置显示页码数
	int navigatePages=3;
	@Override
	public PageBean listPage(AddressVO addressVO) throws Exception{
		//设置分页数据大小
		addressVO.setPageSize(pageSize);
		//查询数目的总个数
		Integer count=addressMapper.count(addressVO);
		if(count==0){
			PageBean pageBean=new PageBean(null,pageSize,addressVO.getCurrentPage(),count,navigatePages);
			//数据库没有数据
			return pageBean;
		}

		//判断页码是否越界,如果页码大于最大值,返回最后一页数据
		//计算最大页码数
		//此时的getCurrentPage是从前端获取到的默认的值为1
		int max= DataUtils.getPageCount(count,pageSize);
		if(addressVO.getCurrentPage()>max){
			addressVO.setCurrentPage(max);
		}

		//设置查询起始位置
		addressVO.setStartRecord(DataUtils.getStartRecord(addressVO.getCurrentPage(),pageSize));

		//调用mapper查询数据
		List<AddressDO> flatsDOS=addressMapper.listPage(addressVO);
		PageBean pageBean=new PageBean(flatsDOS,pageSize,addressVO.getCurrentPage(),count,navigatePages);

		return pageBean;
	}
	
	@Override
	public Result count(AddressVO addressVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteById(Integer id) throws Exception{
		Integer count = addressMapper.deleteById(id);
		if (count > 0) {
			return Result.getSuccess("添加成功");
		}else{
			return Result.getFailure("操作失败");
		}
	}

}